import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:huajianshe_app/base/AppNavigator.dart';
import 'package:huajianshe_app/base/BaseState.dart';
import 'package:huajianshe_app/base/Refresher.dart';
import 'package:huajianshe_app/common/load_image.dart';
import 'package:huajianshe_app/common/res.dart';
import 'package:huajianshe_app/network/Api.dart';
import 'package:huajianshe_app/widget/BgImageLayout.dart';
import 'package:huajianshe_app/widget/RoundedCornerWidget.dart';

import 'LessonItem.dart';
import 'model/LessonModel.dart';

class LessonSpecialListPage extends StatefulWidget {

  final int? categoryId;
  final String? title;
  final int? type; //1.普通课程 2.大师课程 #默认全部
  final int? recommendStatus; //是否热门推荐 0.否 1.是
  final int? isNewSale; //是否最新上架 0.否 1.是

  const LessonSpecialListPage(this.categoryId, this.title, {
    Key? key, this.type, this.recommendStatus, this.isNewSale,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _LessonSpecialListPageState();
  }
}

class _LessonSpecialListPageState extends BaseState<LessonSpecialListPage> {

  int page = 1;
  late SystemUiOverlayStyle overlayStyle;
  late double appbarOpacity;
  late ScrollController controller;
  late int offset = 0;

  @override
  void initState() {
    super.initState();
    overlayStyle = SystemUiOverlayStyle.light;
    appbarOpacity = 0;
    controller = ScrollController();
    controller.addListener(() => {_onScroll()});
    refresher = Refresher<Lesson>(this);
    load(false);
  }

  load(bool doNotShowProgressBar) {
    var data = {
      'list_rows': 10,
      'page': page,
      'lesson_type': widget.type, //课程类型 1.普通课程 2.大师课程 #默认全部
      'category_id': widget.categoryId,
      'recommend_status': widget.recommendStatus,
      'is_new_sale': widget.isNewSale
    };
    sendReq(Api.lessonList(), LessonListResp(), data: data, doNotShowProgressBar: doNotShowProgressBar);

  }

  @override
  void onRefresh() {
    refresher?.mode = Mode.refresh;
    page = 1;
    load(true);
  }

  @override
  void onLoadMore() {
    refresher?.mode = Mode.loadMore;
    load(true);
  }

  @override
  void onReqSuccess<T>(T t, int code, String msg, String path) {
    if (t is LessonListResp) {
      // lessons = t.data?.data;
      refresher?.load(t.data?.data);
      page++;
      setState(() => null);
    }
  }

  void _onScroll() {
    // print('position ${controller.position} offset ${controller.offset}');
    int newOffset = controller.offset.toInt() - 50;
    if (newOffset > 50) newOffset = 50;
    if (newOffset < 0) newOffset = 0;
    if (offset == newOffset) return;
    offset = newOffset;
    this.appbarOpacity = offset.toDouble() * 2 * 0.01;
    print('appbarOpacity ${appbarOpacity} offset ${offset}');
    if (this.appbarOpacity > 0.5 && this.overlayStyle != SystemUiOverlayStyle.dark) {
      this.overlayStyle = SystemUiOverlayStyle.dark;
    }
    if (this.appbarOpacity <= 0.5 && this.overlayStyle != SystemUiOverlayStyle.light) {
      this.overlayStyle = SystemUiOverlayStyle.light;
    }
    setState(() { });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        // appBar: CAppBar(title: widget.title ?? '', overlayStyle: SystemUiOverlayStyle.dark),
        body: //
          BgImageLayout(imageHeight: 190, imageUrl: 'bg_user_info', bgColor: Colors.white, overlayStyle: overlayStyle, children: [
            Padding(padding: EdgeInsets.only(top: 114, left: 15), child: Row(children: [
              Text(widget.title ?? '', style: TS(18, Colors.white),),
              W(6),
              RoundedCornerWidget(width: 38, height: 18, radius: 3, child: Center(child: Text('专栏', style: TS(10, Colors.white))),
                backgroundColor: Colors.transparent, borderWidth: 0, borderColor: Colors.transparent,
                gradient: LinearGradient(colors: [Color(0xFFFE2E2E), Color(0xFFF78C58)], begin: Alignment.topCenter, end: Alignment.bottomCenter),
              ),
            ])),
            Padding(padding: EdgeInsets.only(top: 145),
              child: refresher?.build(buildChild())
            ),
            SafeArea(child: Container(height: 48, child: GestureDetector(onTap: () { AppNavigator.pop(context); },
                child: Padding(padding: EdgeInsets.all(15), child: LoadImage('ic_white_arrow'))
            )))

          ])
    );

  }

  Widget? buildChild() {
    print('buildChild');
    Widget? child = buildEmptyPage();
    if (child != null) {
      print('empty buildChild');
      return child;
    }
    return

      ListView.separated(
          itemBuilder: (_, index) {
            return Padding(padding: EdgeInsets.symmetric(horizontal: 13),
                child: LessonItem(refresher?.data?[index]));
          },
          separatorBuilder: (_, index) => H(15),
          itemCount: (refresher?.data?.length ?? 0)
      );
  }
}